package cn.huapu.power.energy.service.impl;

import cn.huapu.power.energy.bean.*;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.bean.dto.WordDatasDto;
import cn.huapu.power.energy.bean.vo.Ranking5Vo;
import cn.huapu.power.energy.bean.vo.PeaRankVo;
import cn.huapu.power.energy.mapper.PeaIndustryStandardMapper;
import cn.huapu.power.energy.mapper.PeaRankMapper;
import cn.huapu.power.energy.mapper.SysClientEngMapper;
import cn.huapu.power.energy.service.*;
import cn.huapu.power.energy.util.ObjectHelper;
import cn.huapu.power.energy.util.wordUtils.JfImgUtil;
import cn.huapu.power.energy.util.wordUtils.WordAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 测试用户权限服务实现类
 * @author autogen
 *
 */
@Service
public class PeaRankServiceImpl extends BaseServiceImpl<PeaRankMapper, PeaRank,Integer> implements PeaRankService {
    @Autowired
    private PeaRankMapper ankMapper;
    @Autowired
    private PeaIndustryStandardMapper dardMapper;
    @Autowired
    private WordAction wordAction;
    @Autowired
    private PeaUserpowerfeeService feeService;
    @Autowired
    private PeaFeeoptimizeService zeService;
    @Autowired
    private SysClientEngMapper engMapper;

//    @Cacheable(value = "platcache", key = "'ranking_'+ #da.id")
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> ranking( DateParameterBo da) {
        Map<String,Object> map = new LinkedHashMap<>();
        SysClientEng sysClientEng = engMapper.get(da.getId());
        if(sysClientEng != null && sysClientEng.getClientid()!= null){
            da.setTypeName(sysClientEng.getIdstytype());
        }
        //所有用户不分行业排名与电价水平得分
        List<PeaRankVo> listAll = ankMapper.rankingAll(da);
        //所有用户不分行业排名与变损得分
        List<PeaRankVo> listAllbs = ankMapper.rankingAllbs(da);
        //所有用户不分行业排名用电评价总得分
        List<PeaRankVo> listAllz = ankMapper.rankingAllz(da);
        //所有用户不分行业排名功率因数得分
        List<PeaRankVo> listAllbgv = ankMapper.rankingAllgv(da);
        //所有用户不分行业排名电费差得分
        List<PeaRankVo> listAllbgdfc = ankMapper.rankingAlldfc(da);
        //所有用户不分行业排名峰谷差得分
        List<PeaRankVo> listAllbfgc = ankMapper.rankingAllfgc(da);
        map.put("listAll",listAll);
        map.put("listAllbs",listAllbs);
        map.put("listAllz",listAllz);
        map.put("listAllbgv",listAllbgv);
        map.put("listAllbgdfc",listAllbgdfc);
        map.put("listAllbfgc",listAllbfgc);
        //当前用户不分行业排名与电价水平得分
        PeaRank peaRank = ankMapper.rankingUser(da);
        map.put("userRank",peaRank);

        if("1".equals(da.getType1())){
            //行业优良中差
            List<PeaIndustryStandard> levles = dardMapper.levle(da);
            map.put("levlealls",levles);
        }else{
            //所有行业优良中差
            List<PeaIndustryStandard> levlealls = dardMapper.levleAll(da);
            map.put("levlealls",levlealls);
        }

        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Ranking5Vo> ranking_5(DateParameterBo da) {
        SysClientEng sysClientEng = engMapper.get(da.getId());
        if(sysClientEng != null && sysClientEng.getClientid()!= null){
            da.setTypeName(sysClientEng.getIdstytype());
        }
        List<Ranking5Vo> list = ankMapper.ranking_5(da);
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Ranking5Vo> jqRanking(DateParameterBo da) {
        SysClientEng sysClientEng = engMapper.get(da.getId());
        if(sysClientEng != null && sysClientEng.getClientid()!= null){
            da.setTypeName(sysClientEng.getIdstytype());
        }
        List<Ranking5Vo> list = ankMapper.jqRanking(da);

        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public InputStream downlodeWord(DateParameterBo da) throws IOException {
        WordDatasDto wordDatasDto = new WordDatasDto();
        LinkedList<String> linkedList = new LinkedList<>();
        PeaRankVo peaRankVo = new PeaRankVo();
        //当前用户不分行业排名与电价水平得分
        PeaRank peaRank = ankMapper.rankingUser(da);
        wordDatasDto.setPeaRank(peaRank);

        //所有用户不分行业排名与电价水平得分图片字符串
        peaRankVo.setRank(peaRank.getAuavgrank());
        peaRankVo.setScore(peaRank.getAvgscore());
        peaRankVo.setClientname(peaRank.getClientname());
        List<PeaRankVo> listAll = ankMapper.rankingAll(da);
        listAll.add(peaRankVo);
        String imageStr1 = JfImgUtil.getImageStr(JfImgUtil.getImg(listAll));
        linkedList.add(imageStr1);

        //所有用户不分行业排名与变损得分
        peaRankVo.setRank(peaRank.getAutlrank());
        peaRankVo.setScore(peaRank.getTlossscore());
        List<PeaRankVo> listAllbs = ankMapper.rankingAllbs(da);
        listAllbs.add(peaRankVo);
        String imageStr2 = JfImgUtil.getImageStr(JfImgUtil.getImg(listAllbs));
        linkedList.add(imageStr2);

        //所有用户不分行业排名用电评价总得分
        peaRankVo.setRank(peaRank.getAllrank());
        peaRankVo.setScore(peaRank.getComscore());
        List<PeaRankVo> listAllz = ankMapper.rankingAllz(da);
        listAllz.add(peaRankVo);
        String imageStr3 = JfImgUtil.getImageStr(JfImgUtil.getImg(listAllz));
        linkedList.add(imageStr3);

        //所有用户不分行业排名功率因数得分
        peaRankVo.setRank(peaRank.getAuprrank());
        peaRankVo.setScore(peaRank.getPwrratioscore());
        List<PeaRankVo> listAllbgv = ankMapper.rankingAllgv(da);
        listAllbgv.add(peaRankVo);
        String imageStr4 = JfImgUtil.getImageStr(JfImgUtil.getImg(listAllbgv));
        linkedList.add(imageStr4);

        //所有用户不分行业排名电费差得分
        peaRankVo.setRank(peaRank.getAufdrank());
        peaRankVo.setScore(peaRank.getFeediffvalscore());
        List<PeaRankVo> listAllbgdfc = ankMapper.rankingAlldfc(da);
        listAllbgdfc.add(peaRankVo);
        String imageStr5 = JfImgUtil.getImageStr(JfImgUtil.getImg(listAllbgdfc));
        linkedList.add(imageStr5);

        //所有用户不分行业排名峰谷差得分
        peaRankVo.setRank(peaRank.getAufgcrank());
        peaRankVo.setScore(peaRank.getFgcscore());
        List<PeaRankVo> listAllbfgc = ankMapper.rankingAllfgc(da);
        listAllbfgc.add(peaRankVo);
        String imageStr6 = JfImgUtil.getImageStr(JfImgUtil.getImg(listAllbfgc));
        linkedList.add(imageStr6);




        PeaUserpowerfee fee = new PeaUserpowerfee();
        fee.setClientid(da.getId());
        List<PeaUserpowerfee> fees = feeService.queryList(fee);
        //用电类别
        Integer uetype = fees.get(0).getUetype();
        String uetypeStr = this.getUetype(uetype);

        //考核功率因数
        peaRank.setKhcos(fees.get(0).getKhcos());
        peaRank.setSjcos(fees.get(0).getSjcos());

        PeaFeeoptimize peaMin = zeService.queryMinFee(da);
        //用电模式
        wordDatasDto.setUetypeStr(peaMin.getType2());
        //节约电费
        wordDatasDto.setJieyuefee(peaMin.getSumfee());

        //峰谷差



        da.setType2("Actual按需");
        PeaFeeoptimize pea = zeService.queryMinFee(da);
        Double sumfee = pea.getSumfee();
        //可节省电费
        double jieshengfee = sumfee - peaMin.getSumfee();

        wordDatasDto.setUetypeStr(uetypeStr);
        wordDatasDto.setImgList(linkedList);
        wordDatasDto.setJieshengfee(jieshengfee);
        wordAction.createWord(wordDatasDto);
        InputStream wordFile = wordAction.getWordFile();
        return wordFile;
    }

    public String getUetype(int uetype){
        switch(uetype){
            case 10:
                return "大用户直购电";
            case 11:
                return "工商业及其他（大工业）";
            case 12:
                return "工商业及其他（商业）";
            case 13:
                return "工商业及其他（非工业）";
            case 14:
                return "工商业及其他（非居）";
            case 15:
                return "中小学教学用电";
             default:
                 return "大用户直购电";
        }
    }


}
